/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.popup
{
import flash.display.DisplayObject;
import flash.display.Sprite;


public class PopupManager extends Sprite
	{		
		protected static var _instance:PopupManager;
		
		public static function get instance():PopupManager{
			if(!_instance){
				_instance = new PopupManager();
			}
			return _instance;
			
		}
		
		public static var ALERT_OK:uint = 1;
		
		protected var popups:Vector.<IPopup> = new Vector.<IPopup>();
		
		private var isActivPopup:Boolean;
		
		protected function getID():int{
			return popups.length;
		}
		
		public function addPopup(popup:IPopup):int{
			var id:int = getID();
			popups.push(popup);	
			if(!isActivPopup){
				showPopup();
			}			
			return id;
		}
		
		public function removePopup():void{
			if((isActivPopup)&&(popups.length)){
				var popup:IPopup = popups.shift();
				if(popup){
					popup.destroy();
					if(contains(popup as Sprite)){
						removeChild(popup as Sprite);
					}
					popup = null;
				}
			}
			isActivPopup = false;
			if(popups.length){
				showPopup();
			}	
		}
		
		private function showPopup():void{
			if((!isActivPopup)&&(popups.length)){
				var popup:IPopup = popups[popups.length-1];
				if(popup){
					addChild(popup as DisplayObject);							
					popup.show();
					isActivPopup = true;
				}
			}
		}
		
		
		
//		public function alert(text:String, 
//							  title:String = null, 							  
//							  parent:Sprite = null,
//							  centre:Boolean = false,
//							  clickButtonOk:Function = null):int{	
//		var popup:PopupAlert = new PopupAlert(text, title, centre, parent, clickButtonOk);
//		return addPopup(popup);	
//		}
//		
//		public function fishCatch(fish:FishCatchProperty,
//							  parent:Sprite = null,
//							  centre:Boolean = false,
//							  clickButtonOk:Function = null):int{	
//			var popup:PopupCatch = new PopupCatch(fish, parent, centre, clickButtonOk);
//			return addPopup(popup);	
//		}
//		public function fishCatchUni(text:String, 
//								  title:String,
//								  fish:Entity,
//								  parent:Sprite = null,
//								  centre:Boolean = false,
//								  clickButtonOk:Function = null):int{	
//			var popup:PopupCatchUni = new PopupCatchUni(text, title, fish, parent, centre, clickButtonOk);
//			return addPopup(popup);	
//		}
//		public function collection():int{	
//			var popup:PopupCollection = new PopupCollection();
//			return addPopup(popup);	
//		}
//		public function dilogLeft():int{	
//			var popup:PopupDialogLeft = new PopupDialogLeft();
//			return addPopup(popup);	
//		}
//		public function dilogRight():int{	
//			var popup:PopupDialogRight = new PopupDialogRight();
//			return addPopup(popup);	
//		}
//		public function levelUp():int{	
//			var popup:PopupLevelUp = new PopupLevelUp();
//			return addPopup(popup);	
//		}
//		public function social():int{	
//			var popup:PopupSocial = new PopupSocial();
//			return addPopup(popup);	
//		}
	}
}